package com.ccp.dev.form.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.form.model.SysBusEvent;
import com.ccp.dev.form.service.SysBusEventService;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 自定义表单业务数据保存设置 Controller
 *
 * @author chenjing
 */
@Controller
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_FORM + "/sysBusEvent")
@Action(ownermodel = SysAuditModelType.FORM_MANAGEMENT)
public class SysBusEventController extends SysBaseController {

    @Resource
    private SysBusEventService sysBusEventService;

    /**
     * 添加或更新sys_bus_event。
     *
     * @param sysBusEvent sysBusEvent
     * @return ResultData
     */
    @RequestMapping("/save")
    @ResponseBody
    @Action(description = "添加或更新业务保存设置", exectype = "添加或更新日志",
            detail = "添加或更新业务保存设置<#if success>成功<#else>失败</#if>")
    public ResultData save(@RequestBody SysBusEvent sysBusEvent) {
        try {
            SysBusEvent oldSysBusEvent = sysBusEventService.getByFormKey(sysBusEvent.getFormKey());
            String msg = "sysBusEvent.update.success";
            if (oldSysBusEvent == null) {
                msg = "sysBusEvent.add.success";
            }
            sysBusEvent.setPreScript(StringEscapeUtils.unescapeHtml4(sysBusEvent.getPreScript()));
            sysBusEvent.setAfterScript(StringEscapeUtils.unescapeHtml4(sysBusEvent.getAfterScript()));
            sysBusEvent.setJsPreScript(StringEscapeUtils.unescapeHtml4(sysBusEvent.getJsPreScript()));
            sysBusEvent.setJsAfterScript(StringEscapeUtils.unescapeHtml4(sysBusEvent.getJsAfterScript()));
            sysBusEventService.save(sysBusEvent);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess(msg);
        } catch (Exception e) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("sysBusEvent.save.failure");
        }
    }

    /**
     * 查询业务保存设置
     *
     * @param request request
     * @return 编辑页面
     */
    @RequestMapping("/edit")
    @ResponseBody
    @Action(description = "查询业务保存设置", exectype = "查询日志",
            detail = "查询业务保存设置<#if success>成功<#else>失败</#if>")
    public ResultData treeNodeDel(HttpServletRequest request) {
        SysBusEvent sysBusEvent;
        try {
            String formKey = RequestUtil.getString(request, "formKey");
            sysBusEvent = sysBusEventService.getByFormKey(formKey);
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("query.failure");
        }
        return getResultDataSuccess("sysBusEvent.find.success", sysBusEvent);
    }
}
